package com.zekdot.userclient.dao;

import com.zekdot.userclient.entity.UserEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * Create by zekdot on 20-3-13.
 */
@Mapper
@Repository
public interface UserDao {

    int save(UserEntity userEntity);

    int update(UserEntity userEntity);

    @Select("SELECT * FROM user WHERE username=#{username}")
    UserEntity get(@Param("username") String username);

    /**
     * 根据用户名删除用户
     * @param username 用户名
     * @return
     */
    @Delete("DELETE FROM user WHERE username=#{username}")
    int delete(@Param("username") String username);

    int saveUsers(@Param("users") List<UserEntity> users);

    /**
     * 将对应用户名的用户身份更新为2限制其登录
     * @param username 用户名
     * @return
     */
    @Update("UPDATE user SET identify=2 WHERE username=#{username} AND identify=0")
    int stopUser(@Param("username") String username);

    /**
     * 将对应用户名的用户身份更新为0使其能够正常使用
     * @param username 用户名
     * @return
     */
    @Update("UPDATE user SET identify=0 WHERE username=#{username} AND identify=2")
    int restartUser(@Param("username") String username);

    /**
     * 获取更多用户
     * @param lastUsername 最后一条用户名
     * @param length 长度
     * @return
     */
    @Select("SELECT * FROM user WHERE username>#{username} ORDER BY username LIMIT #{length}")
    List<UserEntity> getMoreUsers(@Param("username") String lastUsername,@Param("length") int length);

    @Update("UPDATE user SET password=#{password} WHERE username=#{username}")
    int updatePassword(@Param("username") String username,@Param("password") String newPassword);
}
